package com.nativetalk.controller.app.four;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.TsNtVersionInfo;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.dao.TsNtVersionInfoDao;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangzheng on 2017/3/9.
 */
@Controller
public class FourVersionInfoController {
    private static Logger logger = Logger.getLogger(FourVersionInfoController.class);
    @Resource
    TsNtVersionInfoDao tsNtVersionInfoDao;

    /**
     * @return RetInfo
     * @Purpose 获取版本号信息
     * @version 4.1
     * @time 2017-03-09
     * @author wangzheng
     */
    @RequestMapping(value = "/app/11/get/version", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo versionInfo(@RequestHeader(value = "version", required = true) String version,
                               @RequestHeader(value = "client_type", required = true) String client_type) {
        try {
            //打印入参
            logger.info(Constants.prefix_out + "version:" + version + ",client_type:" + client_type);

            RetInfo retInfo = new RetInfo();
            Map<String, Object> map = new HashMap<>();

            if (StringUtils.isBlank(client_type)) {
                retInfo = RetInfo.getClientErrorInfo("参数client_type不能为空");
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!StringUtils.isNumeric(client_type)) {
                retInfo = RetInfo.getClientErrorInfo("参数client_type必须是数字");
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            Integer clientType = Integer.valueOf(client_type);

            List<TsNtVersionInfo> versionInfos = tsNtVersionInfoDao.findVersionInfoByClientType(clientType);
            String client_version = null;
            String update_flag = "0";

            for (int i = 0; i < versionInfos.size(); i++) {
                TsNtVersionInfo tsNtVersionInfo = versionInfos.get(i);
                if ("version".equals(tsNtVersionInfo.getName().trim())) {
                    client_version = tsNtVersionInfo.getValue().trim();
                }
                if ("update_flag".equals(tsNtVersionInfo.getName().trim())) {
                    update_flag = tsNtVersionInfo.getValue().trim();
                }
            }

            map.put("version", client_version);
            map.put("iosNeedUpdate", Integer.valueOf(update_flag));
            map.put("androidNeedUpdate", Integer.valueOf(update_flag));
            map.put("instruction", "当前app需要更新到最新版本");
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取版本号成功");
            retInfo.setObj(map);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 获取取消预约规则
     * @version 4.1
     * @time 2017-03-14
     * @author wangzheng
     */
    @RequestMapping(value = "/app/11/get/cancel/rule", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo cancelRule() {
        try {
            //打印入参
            logger.info(Constants.prefix_out);

            RetInfo retInfo = new RetInfo();
            Map<String, Object> map = new HashMap<>();
            map.put("cancelRule", "（1）提前四小时可免费取消\n（2）四小时以内取消扣除账户20元\n（3）提前一小时无法取消");
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取取消预约规则成功");
            retInfo.setObj(map);
            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
}
